export default {
  // 为当前模块开启命名空间
  namespaced: true,

  state: () => ({
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    token: uni.getStorageSync('token') || '',
    userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    // 重定向的 object 对象 { openType, from }
    redirectInfo: null
  }),
  mutations: {
    updateAddress(state, address) {
      state.address = address
      this.commit('myUser/saveUser')
    },
    saveUser(state) {
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    updateUserInfo(state, newInfo) {
      state.userinfo = newInfo
      this.commit('myUser/saveUserInfo')
    },
    saveUserInfo(state) {
      uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
    },
    updateUserToken(state, newToken) {
      state.token = newToken
      this.commit('myUser/saveUserToken')
    },
    saveUserToken(state) {
      uni.setStorageSync('token', state.token)
    },
    updateRedirectInfo(state,info) {
      state.redirectInfo = info
    }
  },
  getters: {
    addressStr(state) {
      if (!state.address.provinceName) return ''
      // 拼接 省，市，区，详细地址 的字符串并返回给用户
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    },
  }
}
